<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.domain.User" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码 - 论坛系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 100%;
            max-width: 500px;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .header p {
            opacity: 0.9;
            font-size: 16px;
        }

        .form-container {
            padding: 40px 30px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
            font-size: 14px;
        }

        .form-control {
            width: 100%;
            padding: 15px;
            border: 2px solid #ecf0f1;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            background: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-control.error {
            border-color: #e74c3c;
            background: #fdf2f2;
        }

        .form-control.success {
            border-color: #27ae60;
            background: #f2fdf5;
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #27ae60;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .password-requirements {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            margin-top: 10px;
            font-size: 13px;
        }

        .password-requirements h4 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 14px;
        }

        .requirement {
            display: flex;
            align-items: center;
            margin-bottom: 5px;
            color: #7f8c8d;
        }

        .requirement.valid {
            color: #27ae60;
        }

        .requirement-icon {
            margin-right: 8px;
            font-size: 12px;
        }

        .btn {
            width: 100%;
            padding: 15px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 15px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .btn-primary:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .btn-secondary {
            background: #ecf0f1;
            color: #2c3e50;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-secondary:hover {
            background: #d5dbdb;
            text-decoration: none;
            color: #2c3e50;
        }

        .alert {
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .back-link {
            text-align: center;
            margin-top: 20px;
        }

        .back-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .back-link a:hover {
            text-decoration: underline;
        }

        @media (max-width: 480px) {
            .container {
                margin: 10px;
            }

            .header {
                padding: 30px 20px;
            }

            .form-container {
                padding: 30px 20px;
            }
        }
    </style>
</head>
<body>
<%
    User currentUser = (User) session.getAttribute("USER_SESSION");
    if (currentUser == null) {
        response.sendRedirect("/login");
        return;
    }

    String msg = (String) request.getAttribute("msg");
    String msgType = (String) request.getAttribute("msgType");
%>

<div class="container">
    <div class="header">
        <h1>🔒 修改密码</h1>
        <p>为了您的账户安全，请设置一个强密码</p>
    </div>

    <div class="form-container">
        <% if (msg != null) { %>
        <div class="alert <%= "success".equals(msgType) ? "alert-success" : "alert-error" %>">
            <%= msg %>
        </div>
        <% } %>

        <form id="changePasswordForm" action="/user/changePassword" method="post">
            <div class="form-group">
                <label for="currentPassword">当前密码</label>
                <input type="password" id="currentPassword" name="currentPassword" class="form-control" required>
                <div class="error-message" id="currentPasswordError"></div>
            </div>

            <div class="form-group">
                <label for="newPassword">新密码</label>
                <input type="password" id="newPassword" name="newPassword" class="form-control" required>
                <div class="error-message" id="newPasswordError"></div>
                <div class="success-message" id="newPasswordSuccess"></div>

                <div class="password-requirements">
                    <h4>密码要求：</h4>
                    <div class="requirement" id="lengthReq">
                        <span class="requirement-icon">○</span>
                        <span>至少8个字符</span>
                    </div>
                    <div class="requirement" id="letterReq">
                        <span class="requirement-icon">○</span>
                        <span>包含英文字母</span>
                    </div>
                    <div class="requirement" id="numberReq">
                        <span class="requirement-icon">○</span>
                        <span>包含数字</span>
                    </div>
                    <div class="requirement" id="specialReq">
                        <span class="requirement-icon">○</span>
                        <span>包含特殊字符 (!@#$%^&*)</span>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="confirmPassword">确认新密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" class="form-control" required>
                <div class="error-message" id="confirmPasswordError"></div>
                <div class="success-message" id="confirmPasswordSuccess"></div>
            </div>

            <button type="submit" class="btn btn-primary" id="submitBtn" disabled>
                修改密码
            </button>

            <a href="/user/profile" class="btn btn-secondary">
                返回个人中心
            </a>
        </form>

        <div class="back-link">
            <a href="/user/profile">← 返回个人中心</a>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const form = document.getElementById('changePasswordForm');
        const currentPassword = document.getElementById('currentPassword');
        const newPassword = document.getElementById('newPassword');
        const confirmPassword = document.getElementById('confirmPassword');
        const submitBtn = document.getElementById('submitBtn');

        // 密码要求元素
        const lengthReq = document.getElementById('lengthReq');
        const letterReq = document.getElementById('letterReq');
        const numberReq = document.getElementById('numberReq');
        const specialReq = document.getElementById('specialReq');

        // 错误和成功消息元素
        const currentPasswordError = document.getElementById('currentPasswordError');
        const newPasswordError = document.getElementById('newPasswordError');
        const newPasswordSuccess = document.getElementById('newPasswordSuccess');
        const confirmPasswordError = document.getElementById('confirmPasswordError');
        const confirmPasswordSuccess = document.getElementById('confirmPasswordSuccess');

        // 密码验证函数
        function validatePassword(password) {
            const requirements = {
                length: password.length >= 8,
                letter: /[a-zA-Z]/.test(password),
                number: /\d/.test(password),
                special: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)
            };

            return requirements;
        }

        // 更新要求显示
        function updateRequirements(requirements) {
            updateRequirement(lengthReq, requirements.length);
            updateRequirement(letterReq, requirements.letter);
            updateRequirement(numberReq, requirements.number);
            updateRequirement(specialReq, requirements.special);
        }

        function updateRequirement(element, isValid) {
            const icon = element.querySelector('.requirement-icon');
            if (isValid) {
                element.classList.add('valid');
                icon.textContent = '✓';
            } else {
                element.classList.remove('valid');
                icon.textContent = '○';
            }
        }

        // 检查表单有效性
        function checkFormValidity() {
            const currentPasswordValid = currentPassword.value.length > 0;
            const newPasswordValid = isNewPasswordValid();
            const confirmPasswordValid = isConfirmPasswordValid();

            submitBtn.disabled = !(currentPasswordValid && newPasswordValid && confirmPasswordValid);
        }

        function isNewPasswordValid() {
            const requirements = validatePassword(newPassword.value);
            return requirements.length && requirements.letter && requirements.number && requirements.special;
        }

        function isConfirmPasswordValid() {
            return confirmPassword.value.length > 0 && newPassword.value === confirmPassword.value;
        }

        // 当前密码验证
        currentPassword.addEventListener('input', function() {
            if (this.value.length === 0) {
                showError(this, currentPasswordError, '请输入当前密码');
            } else {
                hideError(this, currentPasswordError);
            }
            checkFormValidity();
        });

        // 新密码验证
        newPassword.addEventListener('input', function() {
            const requirements = validatePassword(this.value);
            updateRequirements(requirements);

            if (this.value.length === 0) {
                hideError(this, newPasswordError);
                hideSuccess(newPasswordSuccess);
            } else if (isNewPasswordValid()) {
                hideError(this, newPasswordError);
                showSuccess(this, newPasswordSuccess, '密码强度良好');
            } else {
                showError(this, newPasswordError, '密码不符合要求');
                hideSuccess(newPasswordSuccess);
            }

            // 如果确认密码已填写，重新验证
            if (confirmPassword.value.length > 0) {
                confirmPassword.dispatchEvent(new Event('input'));
            }

            checkFormValidity();
        });

        // 确认密码验证
        confirmPassword.addEventListener('input', function() {
            if (this.value.length === 0) {
                hideError(this, confirmPasswordError);
                hideSuccess(confirmPasswordSuccess);
            } else if (this.value === newPassword.value) {
                hideError(this, confirmPasswordError);
                showSuccess(this, confirmPasswordSuccess, '密码匹配');
            } else {
                showError(this, confirmPasswordError, '两次输入的密码不一致');
                hideSuccess(confirmPasswordSuccess);
            }
            checkFormValidity();
        });

        // 显示错误
        function showError(input, errorElement, message) {
            input.classList.add('error');
            input.classList.remove('success');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }

        // 隐藏错误
        function hideError(input, errorElement) {
            input.classList.remove('error');
            errorElement.style.display = 'none';
        }

        // 显示成功
        function showSuccess(input, successElement, message) {
            input.classList.add('success');
            input.classList.remove('error');
            successElement.textContent = message;
            successElement.style.display = 'block';
        }

        // 隐藏成功
        function hideSuccess(successElement) {
            successElement.style.display = 'none';
        }

        // 表单提交验证
        form.addEventListener('submit', function(e) {
            if (!isNewPasswordValid() || !isConfirmPasswordValid() || currentPassword.value.length === 0) {
                e.preventDefault();
                alert('请确保所有字段都正确填写');
            }
        });
    });
</script>
</body>
</html>
